package com.wxyzz.canteencommon.common;


import com.wxyzz.canteencommon.entity.sys.SysUser;
import com.wxyzz.canteencommon.exception.CustmerException;
import com.wxyzz.canteencommon.utils.RedisUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/10/14
 * @Solgne 从想做到能做
 * @Desc
 */
@Component
public class LoginInterception implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (token == null || "".equals(token)){
            throw new CustmerException(50008,"无效令牌，请重新登录");
        }
        SysUser value = (SysUser) RedisUtils.getValue(token);
        if (value == null) {
            throw new CustmerException(50014,"身份信息失效，请重新登录");
        }
        return true;
    }
}
